package com.example.ttkl.fragment.XGJActivity;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.ttkl.Base.BaseActivity;
import com.example.ttkl.R;
import com.example.ttkl.fragment.XGJActivity.JSQ.CommonDialogs;
import com.example.ttkl.fragment.XGJActivity.JSQ.ConverterFragment;
import com.example.ttkl.fragment.XGJActivity.JSQ.MainFragment;
import com.example.ttkl.fragment.XGJActivity.JSQ.MyFragmentAdapter;
import com.example.ttkl.interfaces.IBasePresenter;
import com.example.ttkl.util.StatusBarUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class JSQActivity extends BaseActivity {
    private ViewPager mViewPager;
    private MainFragment fragmentMain;
    private ConverterFragment fragmentConverter;
    private int currentTabPos = 0;
    private String currentCalculatorMode = "";
    private String currentConverterMode = "";
    private PopupMenu mainMenu;
    private int tab_text;
    private int tab_text_color_selected;

    private String text_calculator_mode;
    private String text_calculator;
    private String text_converter;
    private Drawable ic_down_small_primary;

    private Button btn_main_choose;
    private Button btn_convert_choose;
    private Button btn_settings;
    @Override
    protected IBasePresenter initPresenter() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_jsq;
    }

    @Override
    protected void initView() {
        StatusBarUtils.setLightMode(this);

        mViewPager = findViewById(R.id.view_pager_main);

        List<String> sTitle = new ArrayList<>();
        sTitle.add("");
        sTitle.add("");

        fragmentMain = MainFragment.newInstance();
        fragmentConverter = ConverterFragment.newInstance();

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(fragmentMain);
        fragments.add(fragmentConverter);

        MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager(), fragments, sTitle);
        mViewPager.setAdapter(adapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentTabPos = position;
                updateTabText();
                updateMainMenuState();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        fragmentMain.setOnPadModeChangedListener((String m) -> {
            currentCalculatorMode = m;
            updateTabText();
        });
        fragmentConverter.setOnModeChangedListener((String m) -> {
            currentConverterMode = m;
            updateTabText();
        });
        initResources();
        initControl();
        initMainMenu();
    }
    private void updateMainMenuState() {
        Menu menu = mainMenu.getMenu();
        switch (currentTabPos) {
            case 0:
                menu.findItem(R.id.action_show_functions).setEnabled(true);
                menu.findItem(R.id.action_custom_input).setEnabled(true);
                menu.findItem(R.id.action_show_calc_step).setEnabled(true);
                menu.findItem(R.id.action_show_full).setEnabled(true);
                break;
            case 1:
                menu.findItem(R.id.action_show_functions).setEnabled(false);
                menu.findItem(R.id.action_custom_input).setEnabled(false);
                menu.findItem(R.id.action_show_calc_step).setEnabled(false);
                menu.findItem(R.id.action_show_full).setEnabled(false);
                break;
        }
    }
    private void initResources() {
        Resources resources = getResources();
        tab_text = resources.getColor(R.color.tab_text, null);
        tab_text_color_selected = resources.getColor(R.color.tab_text_color_selected, null);
        text_calculator_mode = resources.getString(R.string.text_calculator_mode);
        text_calculator = resources.getString(R.string.text_calculator);
        text_converter = resources.getString(R.string.text_converter);
        ic_down_small_primary = resources.getDrawable(R.drawable.ic_down_small_primary, null);
        ic_down_small_primary.setBounds(1, 1, 30, 30);
    }

    private void updateTabText() {
        switch (currentTabPos) {
            case 0:
                btn_main_choose.setTextColor(tab_text_color_selected);
                btn_main_choose.setText(MessageFormat.format(text_calculator_mode, currentCalculatorMode));
                btn_main_choose.setCompoundDrawables(null, null, ic_down_small_primary, null);
                btn_convert_choose.setCompoundDrawables(null, null, null, null);
                btn_convert_choose.setText(text_converter);
                btn_convert_choose.setTextColor(tab_text);
                break;
            case 1:
                btn_main_choose.setTextColor(tab_text);
                btn_main_choose.setText(text_calculator);
                btn_main_choose.setCompoundDrawables(null, null, null, null);
                btn_convert_choose.setCompoundDrawables(null, null, ic_down_small_primary, null);
                btn_convert_choose.setText(currentConverterMode);
                btn_convert_choose.setTextColor(tab_text_color_selected);
                break;
        }
    }

    private void initControl() {
        btn_main_choose = findViewById(R.id.btn_main_choose);
        btn_convert_choose = findViewById(R.id.btn_convert_choose);
        btn_settings = findViewById(R.id.btn_settings);
        btn_main_choose.setOnClickListener((View v) -> {
            if (mViewPager.getCurrentItem() == 0) fragmentMain.chooseMode();
            else mViewPager.setCurrentItem(0);
        });
        btn_convert_choose.setOnClickListener((View v) -> {
            if (mViewPager.getCurrentItem() == 1) fragmentConverter.showChooseConvertDialog();
            else mViewPager.setCurrentItem(1);
        });
        btn_settings.setOnClickListener((v) -> mainMenu.show());
    }

    private void initMainMenu() {
        mainMenu = new PopupMenu(this, btn_settings);
        mainMenu.getMenuInflater().inflate(R.menu.menu_main, mainMenu.getMenu());
        mainMenu.setOnMenuItemClickListener(item -> {
            switch (item.getItemId()) {
                case R.id.action_exit:
                    finish();
                    break;
                case R.id.action_settings:
                    CommonDialogs.showSettings(this);
                    break;
                case R.id.action_show_full:
                    fragmentMain.showFullText();
                    break;
                case R.id.action_show_functions:
                    fragmentMain.showAllFunctionsHelp();
                    break;
                case R.id.action_custom_input:
                    fragmentMain.showCustomerView();
                    break;
                case R.id.action_show_calc_step:
                    fragmentMain.showCalcStep();
                    break;
            }
            return true;
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public void showTips(String tips) {

    }

    @Override
    public void showLoading(int visble) {

    }
}
